﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using GRWL.BZSC.UI.CustomCtrl;

namespace GRWL.BZSC.UI
{


    public partial class EditDeptFrm : BaseFrm
    {

        public int Id { get; set; }
        /// <summary>
        /// 新增调用设定为 true, 编辑调用设定为 false
        /// </summary>
        public bool EditMode { get; set; }

        public TB_Department Tb_Department { get; set; }


        /// <summary>
        /// 编码规则
        /// </summary>
        //public CodeRuleFrm.CodeRule codeRule { get; set; }
        public EditDeptFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
            Tb_Department = new TB_Department();

        }

        private void EditDeptFrm_Load(object sender, EventArgs e)
        {
            if (!EditMode)
            {
                this.uTextBox1.Enabled = false;
                Tb_Department = this.BaseDataProxy.GetDepartment(Id);
                if (Tb_Department != null)
                {
                    this.Text = "编辑部门";
                    this.uTextBox1.Text = Tb_Department.code.ToString();
                    this.uTextBox2.Text = Tb_Department.name;
                    this.uTextBox3.Text = Tb_Department.tel;
                    this.uTextBox4.Text = Tb_Department.fax;

                    if (Tb_Department.master != null)
                    {
                        //SelectItem item = new SelectItem();
                        //item.Name = Tb_Department.master;
                        //item.Key = Tb_Department.master;
                        //item.Value = Tb_Department.master;
                        //this.slcrDept.SetSelected(item);
                    }
                }
            }
            else
            {
                if (this.codeRule.autoCode)
                {
                    //this.uTextBox1.Text = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, this.codeRule.header, this.codeRule.len, !string.IsNullOrEmpty(this.codeRule.header), this.codeRule.autoFill, false);
                    this.uTextBox1.Enabled = false;
                }
                else
                    this.uTextBox1.Enabled = true;
                this.Text = "新增部门";
            }


            //uTextBox1.Text = Tb_Department.id.ToString();
            //uTextBox2.Text = Tb_Department.name;
            //uTextBox3.Text = Tb_Department.tel;
            //uTextBox4.Text = Tb_Department.fax;
            //if (Tb_Department.master != null)
            //{
            //        SelectItem item = new SelectItem();
            //        item.Name = Tb_Department.id.ToString();
            //        item.Key = Tb_Department.id.ToString();
            //        item.Value = Tb_Department.name;
            //        this.slcrDept.SetSelected(item);
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (EditMode)
                this.uTextBox1.Text = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, this.codeRule.header, this.codeRule.len, !string.IsNullOrEmpty(this.codeRule.header), this.codeRule.autoFill, false);

            //Tb_Department.id = uTextBox1.Text;
            Tb_Department.code = uTextBox1.Text;
            Tb_Department.name = uTextBox2.Text;
            Tb_Department.tel = uTextBox3.Text;
            Tb_Department.fax = uTextBox4.Text;
            //if (slcrDept.Tag != null)
            //    Tb_Department.parentId = Convert.ToInt32(slcrDept.Tag);

            if (this.slcrDept.SelectedValue() != null)
            {
                Tb_Department.master = Convert.ToInt32(this.slcrDept.SelectedValue());
            }
            Common.OperateType opType = Common.OperateType.Add;
            try
            {
                if (EditMode)
                {
                    int g = BaseDataProxy.AddDepartment(Tb_Department);
                    if (g > 0)
                    {
                        Tb_Department.id = g;
                        MessageBox.Show("已成功保存记录.");
                    }
                }
                else if (!EditMode)
                {
                    opType = Common.OperateType.Update;
                    if (BaseDataProxy.UpdateDepartment(Tb_Department))
                    {
                        MessageBox.Show("已成功保存记录.");
                    }
                }
            }
            catch (InvalidCastException eMsg)
            {
                MessageBox.Show(eMsg.Message.ToString());
            }
            CommonEventArg<TB_Department> arg = new CommonEventArg<TB_Department>(Tb_Department, opType);
            GRWL.BZSC.Common.EventCommon.OnDeptUdTVEvnt(sender, arg);
            Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void slcrDept_SelectorClick(object sender, EventArgs e)
        {
            UserFrm frm = new UserFrm();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                slcrDept.ValueType = Selector.NodeValueType.Value;
                //slcrDept.Tag = Selector.NodeValueType.Value;
                slcrDept.SetSelected(frm.SelectorItem);

                //tMain.Rows[0]["businessman"] = Common.StringToInt(frm.SelectorItem.Value);
            }
        }
    }
}
